package org.liang.util;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.liang.exception.ResponseCode;
import org.springframework.context.annotation.Configuration;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Configuration //配置类
public class AjaxResult {

    private Integer code = ResponseCode.RESPONSE_CODE_200.getCode();     // 默认200，表示操作成功
    private Boolean success = true;    // 表示当前操作是成功还是失败 true:成功 false:失败
    private String msg = ResponseCode.RESPONSE_CODE_200.getMessage();     // 失败的原因
    private Object data;   // 返回给前端的查询结果对象,是一定不能把类型写死的,因为这个AjaxResult类是公共的,每个controller都是返回AjaxResult类的

    public static AjaxResult success(){
        AjaxResult result = new AjaxResult();
        return result;
    }

    public static AjaxResult success(Object data){
        AjaxResult result = new AjaxResult();
        result.setData(data);
        return result;
    }

    public static AjaxResult success(String msg){
        AjaxResult result = new AjaxResult();
        result.setMsg(msg);
        return result;
    }

    public static AjaxResult success(String msg,Object data){
        AjaxResult result = new AjaxResult();
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    public static AjaxResult error(){
        AjaxResult result = new AjaxResult();
        result.setSuccess(false);
        result.setMsg("操作失败");
        return result;
    }

    public static AjaxResult error(String msg){
        AjaxResult result = new AjaxResult();
        result.setSuccess(false);
        result.setMsg(msg);
        return result;
    }

    public static AjaxResult error(ResponseCode responseCode){
        AjaxResult result = new AjaxResult();
        result.setSuccess(false);
        result.setCode(responseCode.getCode());
        result.setMsg(responseCode.getMessage());
        return result;
    }

    public static AjaxResult error(Integer code, String msg){
        AjaxResult result = new AjaxResult();
        result.setSuccess(false);
        result.setMsg(msg);
        result.setCode(code);
        return result;
    }

    public AjaxResult setData(Object data) {
        this.data = data;
        return this;
    }
}
